package com.haitaoit.chengquan.module.home.adapter.parts;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.home.adapter.forum.ForumCenterImgAdapter;
import com.haitaoit.chengquan.module.home.model.AllEvaModel;
import com.haitaoit.chengquan.ui.equipment.activity.ShowPictureActivity;
import com.haitaoit.chengquan.view.CircleImageView;
import com.haitaoit.chengquan.view.MyGridView;
import com.haitaoit.chengquan.view.QRatingBar;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/10/25.
 */
public class AllEvaAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<AllEvaModel> list;

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public AllEvaAdapter(Context context) {
        this.context = context;
    }

    public List<AllEvaModel> getList() {
        return list;
    }

    public void setList(List<AllEvaModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.all_eva_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ImageLoader.getInstance().displayImage(list.get(position).getHead_portrait(), holder.center_img);
        holder.eva_name.setText(list.get(position).getName());
        holder.eva_time.setText(list.get(position).getAddtime());
        holder.eva_content.setText(list.get(position).getContent());
        holder.eva_spec.setText("规格：" + list.get(position).getSpecifications());
        holder.eva_rat.setRating(Float.parseFloat(list.get(position).getScore()));

        if (list.get(position).getList_img().size() == 0) {
            holder.forum_gridview.setVisibility(View.GONE);
        } else {
            holder.forum_gridview.setVisibility(View.VISIBLE);
            ForumCenterImgAdapter adapter = new ForumCenterImgAdapter(context);
            adapter.setList(list.get(position).getList_img());
            holder.forum_gridview.setAdapter(adapter);
            adapter.setBackCall(new BackCall() {
                @Override
                public void deal(int which, Object... obj) {
                   String uri= (String) obj[0];
                    switch (which){
                        case R.id.icon:
                            Intent intent = new Intent();
                            intent.setClass(parent.getContext(), ShowPictureActivity.class);
                            intent.putExtra("image_uri", uri);
                            parent.getContext().startActivity(intent);

                    }
                    super.deal(which, obj);
                }
            });
        }
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.forum_gridview)
        MyGridView forum_gridview;
        @ViewInject(R.id.center_img)
        CircleImageView center_img;
        @ViewInject(R.id.eva_name)
        TextView eva_name;
        @ViewInject(R.id.eva_time)
        TextView eva_time;
        @ViewInject(R.id.eva_content)
        TextView eva_content;
        @ViewInject(R.id.eva_spec)
        TextView eva_spec;
        @ViewInject(R.id.eva_rat)
        QRatingBar eva_rat;
    }
}
